package test.cmd;

import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Runtime工具类，用于本机执行某些命令<br>
 * 
 * TODO: 重定向标准输入输出和错误流
 * 
 * @author liuhongming
 */
public class RuntimeUtil {
    private static Logger _log = Logger.getLogger(RuntimeUtil.class);

	public static String executeCMD(final String string)  
    {  
        StringBuffer resBuf = new StringBuffer();  
        Runtime rt = Runtime.getRuntime();  
        BufferedReader bufr = null;  
        try  
        {  
            Process p = rt.exec(string);
            int exitVal = p.waitFor();  
            if (0 != exitVal)  
            {  
            	 _log.error("execute CMD : "+string+" ,exitVal = " +exitVal +" failed");
            }  
            bufr = new BufferedReader(new InputStreamReader(p.getInputStream()));  
            String line = null;  
            while ((line = bufr.readLine()) != null)  
            {  
                resBuf.append(line).append("\r\n");  
            }  
        }  
        catch (IOException e)  
        {  
        	_log.error("executeCMD IOException = " +e.getMessage(),e );
        }  
        catch (InterruptedException e)  
        { 
        	_log.error("executeCMD InterruptedException  = " +e.getMessage(),e);
        }  
        finally  
        {  
            if (bufr != null)  
            {  
                try  
                {  
                    bufr.close();  
                }  
                catch (Exception e)  
                {  
                	_log.error("bufr close error",e);
                }  
            }  
        }  
  
        return resBuf.toString();  
    }

    public static String executeCMD(final String[] arr)
    {
        StringBuffer resBuf = new StringBuffer();
        Runtime rt = Runtime.getRuntime();
        BufferedReader bufr = null;
        try
        {
            Process p = rt.exec(arr);
            int exitVal = p.waitFor();
            if (0 != exitVal)
            {
                _log.error("execute CMD : "+arr+" ,exitVal = " +exitVal +" failed");
            }
            bufr = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = bufr.readLine()) != null)
            {
                resBuf.append(line).append("\r\n");
            }
        }
        catch (IOException e)
        {
            _log.error("executeCMD IOException = " +e.getMessage(),e );
        }
        catch (InterruptedException e)
        {
            _log.error("executeCMD InterruptedException  = " +e.getMessage(),e);
        }
        finally
        {
            if (bufr != null)
            {
                try
                {
                    bufr.close();
                }
                catch (Exception e)
                {
                    _log.error("bufr close error",e);
                }
            }
        }

        return resBuf.toString();
    }


    public static void main(String[] args) {
        String srcPath = "C:\\Users\\admin\\Desktop\\123.jpg";
        String destPath = "C:\\Users\\admin\\Desktop\\333.jpg";
//        System.out.println(executeCMD("convert " + srcPath + " -resize " + 120 +"X" + 120 + " " + destPath));
//        System.out.println(executeCMD("convert " + srcPath + " -resize 50% " + destPath));
//        System.out.println(executeCMD("convert C:\\Users\\admin\\Desktop\\123.jpg -resize 120X120 C:\\Users\\admin\\Desktop\\666.jpg"));
//        System.out.println(executeCMD("javac"));

//        String[] arr = new String[]{"cmd","/C","convert " + srcPath + " -resize 50% " + destPath};
        String[] arr = new String[]{"cmd","/C","dir"};
        System.out.println(executeCMD(arr));

    }

    @Test
    public void test01(){
        System.out.println("CPU核数："+Runtime.getRuntime().availableProcessors()+"，运行内存："+Runtime.getRuntime().totalMemory()/1024/1024);
    }
}